package yt.demo.appcoresystem.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yt.demo.appcommondb.base.BaseService;
import yt.demo.appcommondb.dto.SplitPageDTO;
import yt.demo.appcommondb.vo.ConditionVO;
import yt.demo.appcommondb.vo.PageVO;
import yt.demo.appcoresystem.entity.SystemUploadFile;
import yt.demo.appcoresystem.mapper.SystemUploadFileMapper;
import yt.demo.appcoresystem.service.ISystemUploadFileService;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SystemUploadFileService extends BaseService<SystemUploadFile, SystemUploadFileMapper> implements ISystemUploadFileService {

    UrlService urlService;

    @Autowired
    public SystemUploadFileService(SystemUploadFileMapper mapper, UrlService urlService) {
        super(mapper, SystemUploadFile.class);
        this.urlService = urlService;
    }

    @Override
    public PageVO<SystemUploadFile> getCusPageList(SplitPageDTO dto, SystemUploadFile params) {
        PageVO<SystemUploadFile> pageList = getPageList(dto, params, w -> w.orderByDesc(SystemUploadFile::getId), null);
        pageList.getList().stream()
                .peek(v -> v.setFullUrl(urlService.getBucketName(v.getStorageMode()))
                )
                .forEach(v -> {
                });  // 确保流操作执行
        return pageList;
    }

    public SystemUploadFile getFileInfoByHash(String hash) {
        LambdaQueryWrapper<SystemUploadFile> wrapper = new LambdaQueryWrapper<SystemUploadFile>().eq(SystemUploadFile::getHash, hash);
        return mapper.selectOne(wrapper);
    }

    @Override
    protected List<ConditionVO<SystemUploadFile, ?>> whereConditions(SystemUploadFile params) {
        List<ConditionVO<SystemUploadFile, ?>> conditions = new ArrayList<>();
        if (params.getMimeType() != null) {
            conditions.add(ConditionVO.createCondition(SystemUploadFile::getMimeType, "like", params.getMimeType()));
        }
        if (params.getOriginName() != null) {
            conditions.add(ConditionVO.createCondition(SystemUploadFile::getOriginName, "like", params.getOriginName()));
        }
        return conditions;
    }

}
